<?php
namespace WebApper;

// Start PHP user session
	session_start();


abstract class Shortcode extends Base {
	
    /**
     * Define shortcode attribute class properties
     *
     */
	protected $shortcode = '';
	protected $defaults = array();
	protected $fields = array();
	protected $formConfig = array();


    /**
     * Initialize the Shortcode Class
     *
     */
    public function __construct() {
       add_shortcode( $this->shortcode, array($this, 'shortcode') );
	   add_action( 'wp_ajax_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );
	   add_action( 'wp_ajax_nopriv_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );
	}

    /**
     * Handles the add post shortcode
     *
     * @param $atts
     */
    abstract protected function shortcode( $atts );

    /**
     * Checks if a shortcode has required attributes
     *
	 * @param string $usercap
     * @since 1.0
     */
	protected function has_req_attrs( $atts ) {
		// Check for required shortcode attributes
		if ( $atts['id'] == null )
			return 'You must give the shortcode a unique ID, i.e., [shortcode id=\'form-1\']';
		return true;
	}



/* TODO FIX BELOW FUNCTION */

    /**
     * Checks if current user has proper privileges
     *
	 * @param string $usercap
     * @since 1.0
     */
	static protected function user_has_cap( $usercap, $ownercap = null ) {
		if ( $usercap != 'logged_out' && $ownercap != 'logged_out' ) :
			if ( is_user_logged_in() ) :
				global $current_user; get_currentuserinfo(); // Get the current user object
				$single_post = get_post( $_GET['postID'] );
				if ( $current_user->ID == $single_post->post_author && $ownercap !== null ) :  // Check privileges for viewing the user's own posts
					if ( !current_user_can( $ownercap ) ) :
						echo 'You do not have sufficient permissions to access this page.';
					else :
						return true;
					endif;
				else :  // Else check privileges for viewing other user's posts
					if ( !current_user_can( $usercap ) ) :
						echo 'You do not have sufficient permissions to access this page.';
					else :
						return true;
					endif;
				endif;
			else :
				echo 'You must login with the sufficient permissions to access this page.';
			endif;
		else :
			return true;
		endif;
	}

    /**
     * Configure the form obj settings
     *
     * @param array $atts
     * @since 1.0
     */
	protected function config_form( $atts ) {
		$this->formConfig['prevent'] = array('bootstrap', 'jQuery', 'focus');  // Prevents scripts from loading twice
		$this->formConfig['view'] = new \PFBC\View\ModalDataTable;
		$this->formConfig['errorView'] = new \PFBC\ErrorView\Modal;
		$this->formConfig['action'] = admin_url('admin-ajax.php');
		$this->formConfig['ajax'] = 1;
		$this->formConfig['ajaxCallback'] = 'parseResponse' . $atts['id'];
	}

    /**
     * Queries the DB to get fields by fieldset with individual field includes and excludes
     *
	 * @param array $atts
	 * @param string $orderBy
     * @since 1.0
     */
	protected function get_fields( $attrs, $only_fields = false ) {
		global $wpdb;
		// Build sql query string to get fields by fieldset with individual field includes and excludes
		$fieldsTable = $wpdb->prefix . 'web_apper_fields';  // Set DB table name
		$fieldsetsTable = $wpdb->prefix . 'web_apper_fieldsets';  // Set DB table name
		$sql = 'SELECT * FROM ' . $fieldsTable . ', ' . $fieldsetsTable . ' WHERE ' . $fieldsTable . '.fieldset = ' . $fieldsetsTable . '.fieldset_id';
		if ( !$only_fields ) :  // Exclude hidden, html & legend ???
			$sql .= ' AND ( NOT ' . $fieldsTable . '.type = "Hidden" AND NOT ' . $fieldsTable . '.type = "HTML" AND NOT ' . $fieldsTable . '.type = "Legend" )';
		endif;
		// Group SQL cluses?
		if ( !empty($attrs['fieldset']) || !empty($attrs['include']) ) :  // if there is fieldsets or includes,
			$sql .= ' AND (';  // group them together.
		endif;
		// Fieldsets SQL
		if ( !empty($attrs['fieldset']) ) :
			$fieldsets = explode( ',', $attrs['fieldset'] );  // Convert string to array
			$once = true;
			foreach ( $fieldsets as $fieldsetID ) :
				if ( $once ) :  // Don't add 'OR' to sql the first time
					$once = false;
				else :
					$sql .= ' OR';
				endif;
				$sql .= ' ' . $fieldsTable . '.fieldset = "' . trim($fieldsetID) . '"';  // Add the fieldset to the sql string
			endforeach;
		endif;
		// Includes SQL
		if ( !empty($attrs['include']) ) :
			$includes = explode( ',', $attrs['include'] );  // Convert string to array
			$once = true;
			foreach ( $includes as $field_id ) :
				if ( $once && empty($attrs['fieldset']) ) :  // Don't add 'OR' to sql the first time if there are no fieldsets in the string already
					$once = false;
				else :
					$sql .= ' OR';
				endif;
				$sql .= ' ' . $fieldsTable . '.field_id = \'' . trim($field_id) . '\'';  // Add the field to the sql string
			endforeach;
		endif;
		// Close group SQL cluses?
		if ( !empty($attrs['fieldset']) || !empty($attrs['include']) ) :  // if there is fieldsets or includes,
			$sql .= ' )';  // close grouping.
		endif;
		// Excludes SQL
		if ( !empty($attrs['exclude']) ) :
			$sql .= ' AND (';  // Group excludes together
			$excludes = explode( ',', $attrs['exclude'] );  // Convert string to array
			$once = true;
			foreach ( $excludes as $field_id ) :
				if ( $once ) :  // Don't add 'AND' to sql the first time
					$once = false;
				else :
					$sql .= ' AND';
				endif;
				$sql .= ' NOT ' . $fieldsTable . '.field_id = \'' . trim($field_id) . '\'';
			endforeach;
			$sql .= ' )';  // Close grouping
		endif;
		// Order By SQL
		$sql .= ' ORDER BY ' . $fieldsetsTable . '.fieldset_order, ' . $fieldsTable . '.field_order ASC';
		// Perform the query
		$fields = $wpdb->get_results(  // Query the DB for fields
			$sql,
			'ARRAY_A'
		);
		if ( empty( $fields ) ) :
			echo 'No fields were found in the database. Try editing your shortcode to see if it helps.';
		endif;
		$this->fields = $fields;
	}

    /**
     * Returns current page URL.
     *
     * @since 1.0
     */
	static protected function cur_page_url() {
		$pageURL = 'http';
		if ($_SERVER['HTTPS'] == 'on')
			$pageURL .= 's';
		$pageURL .= '://';
		if ($_SERVER['SERVER_PORT'] != '80')
			$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		else
			$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		return $pageURL;
	}

    /**
     * Handles ajax requests
     *
     * @since 1.0
     */
	public function sendAjaxResponse() {
		if ( isset( $_POST['web_apper_form'] ) ) :
			check_ajax_referer( 'WebApperAwesomeness!87', 'web_apper_nonce' );  // Verify the security nonce
			if ( $_POST['web_apper_task'] == 'delete' ) :
				echo json_encode( $this->delete_record() );
				die();  // Prevents ajax from returning a '0'
			endif;
			if ( \PFBC\Form::isValid($_POST['web_apper_form']) ) :
				// The form's submitted data has been validated. Proceed with further processing.
				if ( $_POST['web_apper_task'] == 'add' ) :
					echo json_encode( $this->add_record() );
				elseif ( $_POST['web_apper_task'] == 'update' ) :
					echo json_encode( $this->update_record() );
				endif;
			else :
				\PFBC\Form::renderAjaxErrorResponse($_POST['web_apper_form']);
			endif;
			die();  // Prevents ajax from returning a '0'
		endif;
	}

    /**
     * Return a string to use for the post title
     *
     * @since 1.0
     */
	static protected function setTitle() {
		$title_fields = explode( ',', $_POST['web_apper_titlefield'] );
		foreach ( $title_fields as $field )
			$title .= $_POST[$field] . ' ';
		return $title;
	}

    /**
     * Return an integer to use for the post author
     *
     * @since 1.0
     */
	static protected function setAuthor() {
		// Set a post author ID
		if ( is_user_logged_in() ) :
			global $current_user;
			get_currentuserinfo();  // The user ID is now available via a separate global variable ($user_ID)
		else :
			$user_ID = 1;  // User is not logged in, set $user_ID
		endif;
		return $author;
	}

    /**
     * Save custom fields from $_POST
     *
     * @since 1.0
     */
	static protected function add_custom_fields_by_post($postID) {
		foreach ( $_POST as $fieldID => $fieldVal ) :
			if ( $fieldID != 'form' && $fieldID != 'nonce' && $fieldID != 'action' && $fieldID != 'posttype' && $fieldID != 'titlefield' && $fieldID != 'successsave' && $fieldID != 'failuresave' ) :
				if ( is_array( $fieldVal ) )
					$value = serialize( $fieldVal );
				else
					$value = $fieldVal;
				update_post_meta( $postID, $fieldID, $value );
			endif;
		endforeach;
	}

    /**
     * Save custom fields from array of ids
     *
     * @since 1.0
     */
	static protected function update_custom_fields_by_id( $postID, $fields ) {
		$fields = substr( $fields, 0, -1 );
		$fields = explode( ',', $fields );
		foreach ( $fields as $fieldID ) :
			$value = $_POST[ $fieldID ];
			if ( is_array( $fieldVal ) )
				$fieldVal = serialize( $value );
			else
				$fieldVal = $value;
			update_post_meta( $postID, $fieldID, $fieldVal );
		endforeach;
	}

    /**
     * Echos or JSON encodes a response
     *
	 * @param string $message
	 * @param string $class
	 * @param string $title
     * @since 1.0
     */
	static protected function sendResponse( $message, $class = '', $title = 'Heads Up!', $success = true, $ID = null, $url = null, $data = null ) {
		$htmlalert = '<div class="alert ' . $class . ' fade in"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>' . $title . '</strong> ' . $message . '</div>';
		if ( $_POST['action'] ) :  // If ajax $_POST
			$response = array(
				'success'	=> $success,
				'htmlalert' => $htmlalert,
			);
			if ( $_POST['web_apper_task'] ) :
				$response['task'] = $_POST['web_apper_task'];
			endif;
			if ( $ID !== null ) :
				$response['ID'] = $ID;
			endif;
			if ( $url !== null ) :
				$response['url'] = $url;
			endif;
			if ( $data !== null ) :
				$response['data'] = $data;
			endif;
			return $response;
		else :
			echo $htmlalert;
		endif;
	}	

    /**
     * Convert Yes & No to 1 & 0
     *
	 * @param string $text
     * @since 1.0
     */
	static protected function textToBool( $text ) {
		if ( $text == 'Yes' )
			return true;
		else
			return false;
	}

    /**
     * Convert 1 & 0 to Yes & No
     *
	 * @param boolean $bool
     * @since 1.0
     */
	static protected function boolToText( $bool ) {
		if ( $bool == 1 )
			return 'Yes';
		else
			return 'No';
	}

    /**
     * Checks if saved post meta is serialized
     *
     * @global object $wpdb
	 * @param string $str
     * @since 1.0
     */
	static protected function isSerialized($str) {
		return ($str == serialize(false) || @unserialize($str) !== false);
	}
	
	/**
	 *  Adds a field to an instance of PFBC
	 *
	 * @param array $field
	 * @param object $form
	 * @since 1.0
	 */
	static protected function add_form_field( $field, $form ) {
		// Hidden
			if ( $field['type'] == 'Hidden' ) {
				$form->addElement(new \PFBC\Element\Hidden($field['field_id'], $field['default_value']));
			}
		// HTML
			if ( $field['type'] == 'HTML' ) {
				$form->addElement(new \PFBC\Element\HTML($field['default_value']));
			}
		// Legend
			if ( $field['type'] == 'Legend' ) {
				$form->addElement(new \PFBC\Element\Legend($field['default_value']));
			}
		// Textbox
			if ( $field['type'] == 'Textbox' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'AlphaNumeric' )
					$fieldOpts['validation'] = new Validation\AlphaNumeric;
				elseif ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Textbox($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Password
			if ( $field['type'] == 'Password' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'AlphaNumeric' )
					$fieldOpts['validation'] = new Validation\AlphaNumeric;
				elseif ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Password($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Textarea
			if ( $field['type'] == 'Textarea' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'AlphaNumeric' )
					$fieldOpts['validation'] = new Validation\AlphaNumeric;
				elseif ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Textarea($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Phone
			if ( $field['type'] == 'Phone' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'AlphaNumeric' )
					$fieldOpts['validation'] = new Validation\AlphaNumeric;
				elseif ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Phone($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Search
			if ( $field['type'] == 'Search' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'AlphaNumeric' )
					$fieldOpts['validation'] = new Validation\AlphaNumeric;
				elseif ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Search($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// File
			if ( $field['type'] == 'File' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				$form->addElement(new \PFBC\Element\File($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Select
			if ( $field['type'] == 'Select'  ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Select($field['field_name'], $field['field_id'], $options));
				}
			}
		// Radio
			if ( $field['type'] == 'Radio' ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Radio($field['field_name'], $field['field_id'], $options));
				}
			}
		// Checkbox
			if ( $field['type'] == 'Checkbox' ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Checkbox($field['field_name'], $field['field_id'], $options));
				}
			}
		// Sort
			if ( $field['type'] == 'Sort' ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Sort($field['field_name'], $field['field_id'], $options));
				}
			}
		// Checksort
			if ( $field['type'] == 'Checksort' ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Checksort($field['field_name'], $field['field_id'], $options));
				}
			}
		// Range
			if ( $field['type'] == 'Range' ) {
				$optionPairs = explode( '|', $field['options'] );
				if ( !empty($optionPairs[0]) ) {
					$options = array();
					foreach ( $optionPairs as $optionPair ) {
						$optionVals = explode( ',', $optionPair );
						$options[trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[0]))))] = trim(str_replace("'", "", str_replace('"', "", stripslashes($optionVals[1]))));
					}
					$form->addElement(new \PFBC\Element\Range($field['field_name'], $field['field_id'], $options));
				}
			}
		// Url
			if ( $field['type'] == 'Url' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Url($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Email
			if ( $field['type'] == 'Email' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['placeholder'] = $field['placeholder'];
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Email($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Date

			if ( $field['type'] == 'Date' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\Date($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// DateTime
			if ( $field['type'] == 'DateTime' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\DateTime($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// DateTimeLocal
			if ( $field['type'] == 'DateTimeLocal' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\DateTimeLocal($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Month
			if ( $field['type'] == 'Month' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\Month($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Week
			if ( $field['type'] == 'Week' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\Week($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Time
			if ( $field['type'] == 'Time' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$form->addElement(new \PFBC\Element\Time($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Number
			if ( $field['type'] == 'Number' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Number($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Color
			if ( $field['type'] == 'Color' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Color($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// jQueryUIDate
			if ( $field['type'] == 'jQueryUIDate' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\jQueryUIDate($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// TinyMCE
			if ( $field['type'] == 'TinyMCE' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\TinyMCE($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// CKEditor
			if ( $field['type'] == 'CKEditor' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['longDesc'] = $field['long_desc'];
				if ( $field['validation'] == 'RegExp' )
					$fieldOpts['validation'] = new Validation\RegExp( $field['regex'], $field['error_message'] );
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\CKEditor($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// State
			if ( $field['type'] == 'State' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\State($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Country
			if ( $field['type'] == 'Country' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\Country($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// YesNo
			if ( $field['type'] == 'YesNo' ) {
				$fieldOpts = array();
				if ( $field['required'] == true ) $fieldOpts['required'] = true;
				$fieldOpts['shortDesc'] = $field['short_desc'];
				$fieldOpts['longDesc'] = $field['long_desc'];
				$fieldOpts['value'] = stripslashes($field['default_value']);
				$form->addElement(new \PFBC\Element\YesNo($field['field_name'], $field['field_id'], $fieldOpts));
			}
		// Captcha
			if ( $field['type'] == 'Captcha' ) {
				$form->addElement(new \PFBC\Element\Captcha($field['field_name']));
			}
		// Button
			if ( $field['type'] == 'Button' ) {
				$form->addElement(new \PFBC\Element\Button($field['field_name'], $field['field_id']));
			}
	}		
}

?>